<?php
namespace app\index\controller;
use app\model\AdminModel;
use app\model\MotifModel;
use app\model\ScoreModel;
use think\facade\Request;
use think\facade\View;

class homepage{
    //主页
    public function homepage_list(){
        return View::fetch();
    }
    //新建编辑
    public function homepage_edit(){
        return View::fetch();
    }
    //保存预览
    public function homepage_preview(){
        return View::fetch();
    }
    //分析主页
    public function analyze_list(){
        return View::fetch();
    }
    //问卷分析
    public function homepage_analyze(){

        $motifid = Request::param('motif_id');
//        var_dump($motifid);
        $motif = MotifModel::where('motif_id','=',$motifid)->select();
        $adminid = [];
        foreach($motif as $item){
            array_push($adminid,$item['admin_id']);
        }
        $score = [];
        $name = [];
        for($i = 0;$i<count($adminid);$i++){
            $admin = AdminModel::where('admin_id','=',$adminid[$i])->find();
            array_push($name,$admin);
            $scores = ScoreModel::where('motif_id','=',$motifid)->where('admin_id','=',$adminid[$i])->select();
            array_push($score,$scores);
        }
        $arr = [];
        foreach($score as $item => $value){
            array_push($arr,$value[$item]['scores']);
        }
        $average = 0;
        $sum = 0;
        for($i=0;$i<count($arr);$i++){
            $average +=$arr[$i];
        }
        $average = $average/count($arr);
        $sum = $average;
        $adminname = $name[0]['admin_name'];
//        echo $sum .$average.$adminname;
        View::assign('adminname',$adminname);
        View::assign('average',$average);
        View::assign('sum',$sum);
        return View::fetch();
    }
}